package com.xiaren.common;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.io.IOException;

import javax.swing.ImageIcon;

/**
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class ImageManager {
    public static final int MaxHeight = 100;

    public static ImageIcon IMAGE_TITLE = null;//

    static {
        IMAGE_TITLE = new ImageIcon(IConstant.NOTETITLE);
        IMAGE_TITLE.setImage(IMAGE_TITLE.getImage().getScaledInstance(MaxHeight, MaxHeight,
                Image.SCALE_DEFAULT));
    }

    /**
     * 从剪切板中获取图片
     *
     * @return
     * @throws Exception
     */
    public static Image getImageFromClipboard() throws Exception {
        Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable cc = sysc.getContents(null);
        if (cc == null)
            return null;
        else if (cc.isDataFlavorSupported(DataFlavor.imageFlavor))
            return (Image) cc.getTransferData(DataFlavor.imageFlavor);
        return null;
    }

    /**
     * 从指定的剪切板中获取文本内容
     */
    public static String getClipboardText() throws Exception { // 获取剪切板中的内容
        Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable clipT = sysc.getContents(null);
        if (clipT != null) { // 检查内容是否是文本类型
            if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
                return (String) clipT.getTransferData(DataFlavor.stringFlavor);
        }
        return null;
    }

    /**
     * 从指定的剪切板中获取文本内容
     */
    public static void setClipboardText(String text) throws Exception { // 获取剪切板中的内容
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //得到系统剪贴板
        StringSelection selection = new StringSelection(text);
        clipboard.setContents(selection, null);
    }

    public static void setClipboardImage(final Image image) {
        Transferable trans = new Transferable() {
            @Override
            public Object getTransferData(DataFlavor flavor)
                    throws UnsupportedFlavorException, IOException {
                // TODO Auto-generated method stub
                if (isDataFlavorSupported(flavor)) {
                    return image;
                }
                throw new UnsupportedFlavorException(flavor);
            }

            @Override
            public DataFlavor[] getTransferDataFlavors() {
                // TODO Auto-generated method stub
                return new DataFlavor[]{DataFlavor.imageFlavor};
            }

            @Override
            public boolean isDataFlavorSupported(DataFlavor flavor) {
                // TODO Auto-generated method stub
                return DataFlavor.imageFlavor.equals(flavor);
            }
        };
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
    }

//	public static void setImage(Image image) {
//		Images imgSel = new Images(image);
//		//设置
//		Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
//	}

}
